package com.wlr.blog.web.common.service.impl;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.wlr.blog.web.common.entity.Article;
import com.wlr.blog.web.common.mapper.ArticleMapper;
import com.wlr.blog.web.common.service.ArticleService;
import com.wlr.blog.web.common.utils.ElasticSearchUtils;
import com.wlr.blog.web.common.utils.PageResult;
import com.wlr.blog.web.common.utils.PageUtil;
import com.wlr.blog.web.common.vo.ArchivesVO;
import com.wlr.blog.web.common.vo.ArticleVo;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lrwang
 * @version 1.0
 * @date 2022/11/8 21:35
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    ElasticSearchUtils elasticSearchUtils;

    @Override
    public List<Article> getList() {
        return null;
    }

    /**
     * 根据文章编号获取文章
     *
     * @param id 主键ID
     * @return 文章信息
     */
    @Override
    public ArticleVo getById(Serializable id) {
        Integer newId = (Integer) id;
        Article lastArticle = null, nextArticle = null;
        Article article = articleMapper.selectById(newId);
        if (newId > 1) {
            lastArticle = articleMapper.selectById(newId - 1);
        }
        nextArticle = articleMapper.selectById(newId + 1);
        List<Article> newestArticleList = articleMapper.selectOrderByCreateTime();
        ArticleVo result = new ArticleVo();
        BeanUtils.copyProperties(article, result);
        result.setLastArticle(lastArticle);
        result.setNextArticle(nextArticle);
        result.setNewestArticleList(newestArticleList);
        return result;
    }

    /**
     * 分页查询文章
     *
     * @param article 文章参数
     * @return 分页的文章信息
     */
    @Override
    public PageResult queryPage(Article article) {
        PageUtil.startPage();
        List<Article> articles = articleMapper.selectList(new QueryWrapper<Article>(article));
        return new PageResult(articles);
    }

    /**
     * 获取归档列表
     *
     * @param param 分页参数
     * @return 分页的归档列表
     */
    @Override
    public PageResult getArchivesList(ArchivesVO param) {
        PageUtil.startPage();
        List<Article> records = articleMapper.selectList(Wrappers.emptyWrapper());
        List<ArchivesVO> list = records.stream().map(item -> {
            ArchivesVO archivesVO = new ArchivesVO();
            archivesVO.setTitle(item.getArticleTitle());
            archivesVO.setId(item.getId());
            archivesVO.setCover(item.getArticleCover());
            archivesVO.setColor("#49b1f5");
            archivesVO.setTime(item.getCreateTime());
            return archivesVO;
        }).collect(Collectors.toList());
        return new PageResult(list, (int) new PageInfo(records).getTotal());
    }

    @Override
    public boolean save(Article article) {
        boolean save = super.save(article);
        try {
            elasticSearchUtils.initIndexAndInsert("article", article, Long.parseLong(String.valueOf(article.getId())));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return save;
    }
}
